import dd from 'gdt-jsapi'
import * as services from '../../services/index'
import globalData from '../../globalData'

Page({
  data: {
    imageHost: globalData.imageHost,
    userInfo: {},
    remediationList: []
  },
  onShow(query) {
    this.getUserInfo()
  },
  async getUserInfo() {
    const userInfo = await services.getUserInfo()
    this.setData({ userInfo })
    this.getRemediationList(userInfo)
  },
  async getRemediationList(userInfo) {
    dd.showLoading({ text: "加载中" })
    const { data } = await services.getRemediationByCode({
      employeeCode: userInfo.employeeCode,
      realmId: userInfo.realmId
    })
    dd.hideLoading()
    this.setData({
      remediationList: data.map(item => ({
        ...item,
        statusValue: globalData.recordStatusValue(item.status),
        totalDeductMarks: (item.remediationDetail || []).reduce((total, item) => total = total + Number(item.deductMarks), 0)
      }))
    })
  },
  onItemClick({ index }) {
    const url = `/pages/recordDetail/index?remediationId=${this.data.remediationList[index].remediationId}`
    dd.navigateTo({ url })
  },
});
